
/*
 * @Title: HttpClientFluentStu.java
 * @Package
 * @Description: TODO
 * @author tbc
 * @date 2016年7月3日 上午5:04:26
 * @version
 */
package tbg.http;

import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.gson.Gson;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.nio.charset.Charset;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

/**
 * 东方小康出口
 * addr: http://221.5.187.78/DFSKINTERDCS/
 * user: admin
 * pass: 8312wang
 */
public class DFXKRest {

    private static int n = 1;

    /**
     * addHeader("Host", "amdms.foton.com.cn"); :
     * :queryData.start=0&queryData.limit=10&queryData.vehicleStatus=&queryData.
     * orgaId=&queryData.stockDaysEnd=&queryData.stockDays=&queryData.
     * productGroup=&queryData.dealerId=&queryData.vin=LRDV6PDB6EH001452
     */
    public static void main(String[] args) throws IOException {
        List<String> vinS = Files.readLines(getVinFile(), Charset.forName("UTF-8"));
        List<Map<String, Object>> outList = request(vinS);
        System.out.println("请求全部完成，正在输出到excel文件");
        File outFile = getOutputFile();
        System.out.println("全部工作完成...");
    }


    private static List<Map<String, Object>> request(List<String> vinS) {
        String body = getBody();
        String cookie = getCookie();

        List<Map<String, Object>> outList = Lists.newArrayList();

        vinS.forEach(it -> {
            System.out.println("发送第" + n++ + "次请求, time = " + getNowTime());
            Request request = getInitRequest();
            String resultjson = null;
            try {
                resultjson = request.addHeader("Cookie", cookie)//
                        .bodyString(body + it, ContentType.APPLICATION_FORM_URLENCODED)//
                        .execute()//
                        .returnContent()//
                        .asString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("->请求完成, time = " + getNowTime());
            //
            // System.out.println("content : \n" + resultjson);
            Result result = new Gson().fromJson(resultjson, Result.class);
            List<Map<String, Object>> list = result.getList();
            Map<String, Object> row = list.get(0);
            outList.add(row);
            // 打印看看
            String vin = row.get("vin").toString();
            String productGroupName = row.get("productGroupName").toString();
            String znbbm = row.get("znbbm").toString();

            System.out.println("vin: " + vin);
            System.out.println("产品组: " + productGroupName);
            System.out.println("内部编号: " + znbbm);
        });
        return outList;
    }

    private static void excel(List<Map<String, Object>> dataList, File outFile) throws IOException {
        XSSFWorkbook book = new XSSFWorkbook();
        XSSFSheet sheet = book.createSheet("sheet1");

        for (int i = 0; i < dataList.size(); i++) {
            Map<String, Object> _row = dataList.get(i);
            XSSFRow row = sheet.createRow(i);

            XSSFCell c1 = row.createCell(0);
            c1.setCellValue(_row.get("vin").toString());
            XSSFCell c2 = row.createCell(1);
            c2.setCellValue(_row.get("productGroupName").toString());
            XSSFCell c3 = row.createCell(2);
            c3.setCellValue(_row.get("znbbm").toString());

        }
        OutputStream os = new FileOutputStream(outFile);
        book.write(os);
        os.flush();
        os.close();
    }

    public static Request getInitRequest() {
        return Request.Post("http://221.5.187.78/DFSKINTERDCS/shipment/payout/ShipmentQueryAction/listQuery.json?curPage=1&json={}")
                .addHeader("Host", "221.5.187.78")
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0")
                .addHeader("Accept", "text/javascript, text/html, application/xml, text/xml, */*")
                .addHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
                .addHeader("Accept-Encoding", "gzip, deflate")
                .addHeader("X-Requested-With", "XMLHttpRequest")
                .addHeader("X-Prototype-Version", "1.6.0")
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Referer", "http://221.5.187.78/DFSKINTERDCS/shipment/payout/ShipmentQueryAction/forwardList.do")
//                .addHeader("Content-Length", "192")
                .addHeader("Cookie", "userName=admin; language=80751001; browser=Firefox; JSESSIONID=0000Ym8T4AGioXi38TAOtWw5xSq:1a4uko1b2")
                .addHeader("Connection", "keep-alive");
    }

    private static File getVinFile() {
        return new File("f:\\Users\\tbc\\Desktop\\vin.txt");
    }

    private static File getOutputFile() {
        return new File("f:\\Users\\tbc\\Desktop\\amdms.xlsx");
    }

    private static String getCookie() {
        return "style=blue; " +
                "username=dmsadmin@foton.com.cn; " +
                "JSESSIONID=hl3ZY2vfwk5vK2lhlRmch3KhttXqnbLShGhTnPk7vsknVSDThJx5!352727231";
    }

    private static String getBody() {
        return "common=1&start_date=2016-12-01" +
                "&end_date=2016-12-31" +
                "&ship_code=" +
                "&ship_type=" +
                "&dealer_name=" +
                "&dealer_id=" +
                "&dealer_code=" +
                "&sel_status=" +
                "&fin_nopass_status=" +
                "&order_no=" +
                "&input_dealer_code=" +
                "&orderCol=" +
                "&order=" +
                "&go=Go";
    }

    private static String getNowTime() {
        return LocalTime.now().toString();
    }
}